<template>
  <el-dialog
    :title="!dataForm.workno ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="工单号" prop="workid">
      <el-input v-model="dataForm.workid" placeholder="工单号"></el-input>
    </el-form-item>
    <el-form-item label="船号" prop="shipid">
      <el-input v-model="dataForm.shipid" placeholder="船号"></el-input>
    </el-form-item>
    <el-form-item label="分段号" prop="segmentid">
      <el-input v-model="dataForm.segmentid" placeholder="分段号"></el-input>
    </el-form-item>
    <el-form-item label="计件项目" prop="program">
      <el-input v-model="dataForm.program" placeholder="计件项目"></el-input>
    </el-form-item>
    <el-form-item label="预计总工时" prop="prehour">
      <el-input v-model="dataForm.prehour" placeholder="预计总工时"></el-input>
    </el-form-item>
    <el-form-item label="总数量" prop="allnum">
      <el-input v-model="dataForm.allnum" placeholder="总数量"></el-input>
    </el-form-item>
    <el-form-item label="预计项目总金额" prop="preamount">
      <el-input v-model="dataForm.preamount" placeholder="预计项目总金额"></el-input>
    </el-form-item>
    <el-form-item label="计划完成日期" prop="pretime">
      <el-input v-model="dataForm.pretime" placeholder="计划完成日期"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="empname">
      <el-input v-model="dataForm.empname" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="员工ID" prop="empid">
      <el-input v-model="dataForm.empid" placeholder="员工ID"></el-input>
    </el-form-item>
    <el-form-item label="工单状态" prop="workstate">
      <el-input v-model="dataForm.workstate" placeholder="工单状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createtime">
      <el-input v-model="dataForm.createtime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          workno: 0,
          workid: '',
          shipid: '',
          segmentid: '',
          program: '',
          prehour: '',
          allnum: '',
          preamount: '',
          pretime: '',
          empname: '',
          empid: '',
          workstate: '',
          createtime: ''
        },
        dataRule: {
          workid: [
            { required: true, message: '工单号不能为空', trigger: 'blur' }
          ],
          shipid: [
            { required: true, message: '船号不能为空', trigger: 'blur' }
          ],
          segmentid: [
            { required: true, message: '分段号不能为空', trigger: 'blur' }
          ],
          program: [
            { required: true, message: '计件项目不能为空', trigger: 'blur' }
          ],
          prehour: [
            { required: true, message: '预计总工时不能为空', trigger: 'blur' }
          ],
          allnum: [
            { required: true, message: '总数量不能为空', trigger: 'blur' }
          ],
          preamount: [
            { required: true, message: '预计项目总金额不能为空', trigger: 'blur' }
          ],
          pretime: [
            { required: true, message: '计划完成日期不能为空', trigger: 'blur' }
          ],
          empname: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          empid: [
            { required: true, message: '员工ID不能为空', trigger: 'blur' }
          ],
          workstate: [
            { required: true, message: '工单状态不能为空', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.workno = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.workno) {
            this.$http({
              url: this.$http.adornUrl(`/worklistinfo/worklistinfo/info/${this.dataForm.workno}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.workid = data.worklistinfo.workid
                this.dataForm.shipid = data.worklistinfo.shipid
                this.dataForm.segmentid = data.worklistinfo.segmentid
                this.dataForm.program = data.worklistinfo.program
                this.dataForm.prehour = data.worklistinfo.prehour
                this.dataForm.allnum = data.worklistinfo.allnum
                this.dataForm.preamount = data.worklistinfo.preamount
                this.dataForm.pretime = data.worklistinfo.pretime
                this.dataForm.empname = data.worklistinfo.empname
                this.dataForm.empid = data.worklistinfo.empid
                this.dataForm.workstate = data.worklistinfo.workstate
                this.dataForm.createtime = data.worklistinfo.createtime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/worklistinfo/worklistinfo/${!this.dataForm.workno ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'workno': this.dataForm.workno || undefined,
                'workid': this.dataForm.workid,
                'shipid': this.dataForm.shipid,
                'segmentid': this.dataForm.segmentid,
                'program': this.dataForm.program,
                'prehour': this.dataForm.prehour,
                'allnum': this.dataForm.allnum,
                'preamount': this.dataForm.preamount,
                'pretime': this.dataForm.pretime,
                'empname': this.dataForm.empname,
                'empid': this.dataForm.empid,
                'workstate': this.dataForm.workstate,
                'createtime': this.dataForm.createtime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
